package me.haui;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.logging.Logger;

public class Main {

    private final static Logger log = Logger.getLogger(Main.class.getName());




    public static void main(String[] args) throws IOException {

        log.finest("Start");

        StringBuilder stringBuilder = new StringBuilder();

        List<String> targets = Targets.get();

        log.fine("Number addresses " + String.valueOf(targets.size()));

        final String currentWorkingDir = getNiktoDirectoryName();
        executeOSCommand("mkdir " + getNiktoDirectoryName());


        for (String command : Targets.get()){


            writeToNewFile( currentWorkingDir
                    ,command + ".txt"
                    ,executeOSCommand("nikto -timeout 1 -host " + command) // + " -output ./" + getNiktoDirectoryName() + "/" + command + ".txt");
                    );

        }


        log.finest("Done");

    }


    private static String executeOSCommand(String osCommand) {

        StringBuilder stringBuilder = new StringBuilder();

        try
        {

            log.finest("OS Command " + osCommand);

            Process p=Runtime.getRuntime().exec(osCommand);
            BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream()));

            String line=reader.readLine();

            while(line!=null)
            {
                stringBuilder.append("\n" + line.toString());
                line=reader.readLine();
            }

            p.waitFor();

            log.info("Return of command: " + stringBuilder.toString());

            //log.info("OS Command finished");

        }
        catch(IOException e1) {
            log.severe(e1.toString());
        }
        catch(InterruptedException e2) {
            log.severe(e2.toString());
        }

        return stringBuilder.toString();

    }


    private static void writeToNewFile(String dirName, String filename, String stringToWrite){


        File file = new File( dirName,filename);


        FileOutputStream fileOutputStream = null;

        try {
            file.createNewFile();
            fileOutputStream = new FileOutputStream(file, true);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        FileChannel fileChannel = fileOutputStream.getChannel();
        ByteBuffer byteBuffer = null;
        String messageToWrite = null;


        try {
            fileChannel.write(ByteBuffer.wrap(stringToWrite.getBytes()));
            fileChannel.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }



    public static final String getNiktoDirectoryName(){

        final Calendar cal = Calendar.getInstance();

        final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY_MM_d_HH_mm_ss");

        return new StringBuilder().append("Nikto_run_of_").append(simpleDateFormat.format( cal.getTime()).toString()).toString() ;

    }


}

